package com.ztz.mynewdemo.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.WindowManager;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.adapter.DynamicAdapter;
import com.ztz.mynewdemo.utils.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DynaActivity extends BaseActivity {

    @BindView(R.id.rl_dynamic)
    RecyclerView rlDynamic;
    @BindView(R.id.smart_dynamic)
    SmartRefreshLayout smartDynamic;
    DynamicAdapter adapter;
    List<AVObject> avObjectList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_dyna);
        ButterKnife.bind(this);
        setToolbar("动态");
        avObjectList=new ArrayList<>();
        rlDynamic.setLayoutManager(new LinearLayoutManager(this));
        adapter=new DynamicAdapter(this,avObjectList,R.layout.item_dynamic);
        rlDynamic.setAdapter(adapter);
        smartDynamic.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                smartDynamic.finishRefresh(1000);
                setRefreshThemeColor();
                initData();
            }
        });
        smartDynamic.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                smartDynamic.finishLoadMore(1000);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        AVAnalytics.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        AVAnalytics.onResume(this);
        initData();
    }
    private int themeCount;
    private void setRefreshThemeColor() {
        themeCount++;
        if (themeCount % Constants.FOUR == Constants.ONE) {
            smartDynamic.setPrimaryColorsId(Constants.BLUE_THEME, R.color.white);
        } else if (themeCount % Constants.FOUR == Constants.TWO) {
            smartDynamic.setPrimaryColorsId(Constants.GREEN_THEME, R.color.white);
        } else if (themeCount % Constants.FOUR == Constants.THREE) {
            smartDynamic.setPrimaryColorsId(Constants.RED_THEME, R.color.white);
        } else if (themeCount % Constants.FOUR == Constants.ZERO) {
            smartDynamic.setPrimaryColorsId(Constants.ORANGE_THEME, R.color.white);
        }
    }
    public void initData()
    {
        avObjectList.clear();
        AVQuery<AVObject> avQuery=new AVQuery<>("Product");
        avQuery.orderByAscending("createdAt");
        avQuery.include("owner");
        avQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(e==null)
                {
                    avObjectList.addAll(list);
                    adapter.notifyDataSetChanged();
                }
                else
                {
                    e.printStackTrace();
                }
            }
        });
    }
}
